<?php


namespace app\api\controller;

use app\api\model\AttributeKey;
use app\api\model\Cart;
use app\api\model\Goods;
use app\api\model\GoodsCategory;

class Product extends BaseController
{

    /**
     * 获取全部商品分类
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProCategoryAll()
    {
        $pros = new  GoodsCategory();
        $proc = $pros->getCategory();
        return $this->renderSuccess(compact('proc'));
    }

    /**
     * 获取全部商品
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function getProAll()
    {
        $pros = new Goods();
        $proall = $pros->getGoods();
//        var_dump($proall[1]['imgs'][1]['images_url']);
//        exit();
        return $this->renderSuccess(compact('proall'));
    }

    /**
     * 获取推荐商品
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHotPro()
    {
        $pros = new Goods();
        $recgoods = $pros->getRecGoods();

        return $this->renderSuccess(compact('recgoods'));
    }

    /**
     * 获取指定商品分类id下的商品
     *
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function getProCategory()
    {
        $id = $this->request->param();
        $id = 5;
        $catePro = new Goods();
        $pro = $catePro->getCatePro($id);
        return $this->renderSuccess(compact('pro'));
    }

    /**
     * 搜索商品 根据商品标题搜索匹配内容
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function searchGoods()
    {
        $data = $this->request->param();
        $data = '测试';
        $search = new Goods();
        $searchgoods = $search->searchgood($data);
        return $this->renderSuccess(compact('searchgoods'));
    }

    /**
     * 获取到商品详情
     *
     * @return array|\think\response\Json
     */

    public function proDetail()
    {
        $pro_id = $this->request->param();
        $pro_id = 1;
        $details = new Goods();
        $detail = $details->details($pro_id);
        if($detail == false){
            return $this->renderError('商品不存在或者已下架');
        }
        return $this->renderSuccess(['detail' => $detail]);
    }

//    /**
//     * 获取商品规格
//     *
//     */
//    public function getProSpec()
//    {
//        $pro_id = $this->request->param();
//        $pro_id = 2;
//        $details = new AttributeKey();
//        $detail = $details->getSpecsKey();
//        // var_dump($detail[0]['attribute_key']['attribute_value']);
//        return $this->renderSuccess(compact('detail'));
//    }

    /**
     * 指定商品加入购物车
     */
    public function joinCart()
    {
        $user_id = session('user');
        //获取到指定商品id
        $pro_id = $this->request->param();
        $cart = new Cart();
        $res = $cart->joinCart($user_id, $pro_id);
    }
}